package com.doctor.windflower_doctor.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.os.Handler;
import android.os.Message;
import android.util.SparseArray;

import com.doctor.windflower_doctor.entity.QuestionBeen;
import com.lidroid.xutils.util.LogUtils;

/**
 * <p>
 * 功能 定时器工具类
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年11月13日 下午3:46:06
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class TimerManager {
	private Handler mHandler;
	private SparseArray<QuestionBeen> questionBeens = new SparseArray<QuestionBeen>();
	private static TimerManager instance;
	private final static Object syncObj = new Object();
	private ExecutorService executorService;
	// 用来管理所有下载任务
	private ArrayList<TimerTask> taskList = new ArrayList<TimerTask>();

	private TimerManager() {
		// 最多只能同时下载3个任务，其余的任务排队等待
		executorService = Executors.newFixedThreadPool(50);
	}

	public static TimerManager getInstance() {
		if (null == instance) {
			synchronized (syncObj) {
				instance = new TimerManager();
			}
			return instance;
		}
		return instance;
	}

	public void setHandler(Handler handler) {
		this.mHandler = handler;
	}

	public void startTimer(QuestionBeen questionBeen) {
		if (questionBeens != null && (questionBeens.indexOfValue(questionBeen)) < 0) {
			// LogUtils.e("============startTimer===========");
			questionBeens.put(questionBeen.index, questionBeen);
			TimerTask task = new TimerTask(questionBeen.index);
			taskList.add(task);
			executorService.submit(task);
		}
	}

	public void stopTimer() {
		// LogUtils.e("============stopTimer===========");
		while (taskList.size() != 0) {
			TimerTask task = taskList.remove(0);
			// 可以在这里做其他的处理
			task.stopTask();
		}
		questionBeens.clear();
		// 会停止正在进行的任务和拒绝接受新的任务
		// executorService.shutdownNow();
	}

	public void deleteTimer(QuestionBeen questionBeen) {
		questionBeens.remove(questionBeen.index);
	}

	class TimerTask implements Runnable {
		private int index;
		private boolean isWorking = false;

		public TimerTask(int index) {
			this.isWorking = true;
			this.index = index;
		}

		public void stopTask() {
			this.isWorking = false;
		}

		@Override
		public void run() {
			QuestionBeen questionBeen = questionBeens.get(index);
			while (isWorking) {
				if (questionBeen.timeState == 1) {
					questionBeens.remove(index);
					taskList.remove(index);
					isWorking = false;
					break;
				}
				if (questionBeen.timeCurrentLimit <= questionBeen.restOfTime) {
					try {
						Thread.sleep(1000);
						// LogUtils.e("===========questionBeen.timeCurrentLimit=========2222===="
						// + questionBeen.timeCurrentLimit);
						questionBeen.timeCurrentLimit = questionBeen.timeCurrentLimit - 1;
						this.update(questionBeen);
					} catch (InterruptedException e) {
						e.printStackTrace();
						questionBeens.remove(index);
						questionBeen.timeState = 1;
						isWorking = false;
						break;
					}
				}
			}
		}

		// 更新listview中对应的item
		public void update(QuestionBeen questionBeen) {
			Message msg = mHandler.obtainMessage();
			if (questionBeen.timeCurrentLimit == 0)
				questionBeen.timeState = 1;
			msg.obj = questionBeen;
			msg.sendToTarget();
		}
	}

}
